package com.itwn.comic.web;


import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.User;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.UserService;
import com.itwn.comic.utils.JwtUtil;
import com.itwn.comic.vo.JwtInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.beans.Transient;
import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    UserService userService ;

    /**
     *
     * @param user
     * @return
     *
     * 用户的登录功能
     */
    @PostMapping("/userLogin")
    public ResponseResult<User> userLogin(User user){

        ResponseResult<User> result = new ResponseResult<>() ;
        try {
            User loginUser = userService.userLoginByName(user.getUsername()) ;
            if (loginUser == null){
                result.setCode(201);
                result.setMsg("抱歉,没有此用户信息,请先注册");
            }else {
                if (loginUser.getPassword().equals(user.getPassword())){
                    JwtInfo jwtInfo = new JwtInfo();
                    jwtInfo.setUserName(user.getUsername());
                    String token = JwtUtil.creatToken(jwtInfo);
                    result.setCode(200);
                    result.setMsg("恭喜你,登录成功");
                    result.setData(loginUser);
                    result.setToken(token);
                }else {
                    result.setCode(201);
                    result.setMsg("抱歉,密码错误,请确认后重试");
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,系统错误,请稍后重试");
        }

        return result ;
    }
    /**
     * 用户的列表显示
     */

    @GetMapping("/userList")
    public ResponseResult<PageInfo<User>> userList(@RequestParam(value = "now" , defaultValue = "1") Integer now ,
                                                   @RequestParam(value = "size" , defaultValue = "2") Integer size ,
                                                   String username){
        ResponseResult<PageInfo<User>> result = new ResponseResult<>() ;

        try {
            PageInfo<User> pageInfo = userService.queryUserByPage(now , size , username) ;
            result.setCode(200);
            result.setMsg("恭喜你,查询成功");
            result.setData(pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,查询失败");
        }
        return result ;
    }

    /**
     * 用户的添加功能
     *
     */

    @PostMapping("/addUser")
    public ResponseResult<Void> addUser(@RequestBody User user){
        ResponseResult<Void> result = new ResponseResult<>() ;

        try {
            userService.addUser(user);
            result.setCode(200);
            result.setMsg("恭喜你,添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,添加失败");
        }
        return result ;
    }

    /**
     *
     * @param user
     * @return
     *
     * 用户的修改功能
     */
    @PutMapping("/modifyUser")
    public ResponseResult<Void> modifyUser(@RequestBody User user){
        ResponseResult<Void> result = new ResponseResult<>() ;

        try {
            userService.modifyUser(user) ;
            result.setCode(200);
            result.setMsg("恭喜你,修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,修改失败");
        }
        return result ;
    }

    /**
     *
     * @param id
     * @return
     * 用户的删除功能
     */
    @DeleteMapping("/removeUser")
    public ResponseResult<Void> removeUserByKey(int id){
        ResponseResult<Void> result = new ResponseResult<>() ;
        try {
            userService.removeUserById(id) ;
            result.setCode(200);
            result.setMsg("恭喜你,删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,删除失败,请稍后重试");
        }

        return result ;
    }

    @RequestMapping("/lock")
    public ResponseResult<Void> lock(Integer uid , Integer status){
        ResponseResult<Void> result = new ResponseResult<>() ;

        try {
            userService.modifyUserStatusByCons(uid , status) ;
            result.setCode(200);
            result.setMsg("状态修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("抱歉,状态修改失败");
        }
        return result ;
    }

    /**
     * 头像上传
     * @param multipartFile
     * @return
     */
    @RequestMapping("uploadImg")
    public ResponseResult<String> uploadImg(MultipartFile multipartFile){
        ResponseResult<String> result = new ResponseResult<>();
        try {
            String fileName=UUID.randomUUID().toString()+".jpg";
            String filePath="D:/Imgs/headImg/"+fileName;
            System.out.println(filePath);
            File file=new File(filePath);
            multipartFile.transferTo(file);
            result.setCode(200);
            result.setData(fileName);
            result.setMsg("图片添加成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("添加失败");
            result.setCode(201);
        }
        return result;
    }
}
